<?php
class Control_Article_Call extends QUI_Control_Abstract {
    function render() {
        //                dump($this);
        $user = Helper_RBAC::get('user');
        //        dump($user->call_columns);
        if(!$user->id()) return '';
        FUNC::getChildren(Column::getFormCache(null, $this->_attrs['col_id']), $childrens, array('col_id'), false);    //查找孩子
        $childrens[] = $this->_attrs['col_id'];
        //dump($childrens);
        if(!$this->_attrs['no_role']) {
            // 寻找子栏目id
            //            if($this->_attrs['by_role']) {
            foreach($user->roles as $key=>$role) {
                foreach($role->call_columns as $key=>$column) {
                    //                    $cols[] = $column->col_id;
                    FUNC::getChildren(Column::getFormCache(null, $column->col_id), $nodes, array('col_id'), false);
                    $nodes[] = $column -> col_id;
                }
            }
            //            }
            //dump($user->department->call_columns);
            //exit;

            //            /*
            //按部门调用栏目
            //            if($this->_attrs['by_department']) {
            foreach($user->department->call_columns as $key=>$column) {
                FUNC::getChildren(Column::getFormCache(null, $column->col_id), $nodes, array('col_id'), false);
                $nodes[] = $column -> col_id;
            }
            //            }

            //用户指定调用的栏目
            if($user->call_columns) {
                foreach($user->call_columns as $key=>$column) {
                    FUNC::getChildren(Column::getFormCache(null, $column->col_id), $orcols, array('col_id'), false);
                    $orcols[] = $column->col_id;
                }
            }
            //*/

            //            dump($nodes);
            //            dump($childrens);

            if($nodes) {
                foreach($childrens as $d) {
                    if(in_array($d, $nodes)){
                        $col[] = $d;
                    }
                }
                //                dump($col);
                //                            foreach($nodes as $n) {
                //                                if(in_array($n, $cols))
                //                            }
                //                            $col = array_merge($cols, $nodes);
                //                            dump(array_unique($col));
                //                if($col){
                $condi[] = new QDB_Expr(
                    'col_id in('.implode(',', $childrens). ')'
                );
                //                }
            }
            else {
                $condi[] = new QDB_Expr(
                    'col_id in('.implode(',', $childrens). ')'
                );
            }

            //dump($user->roles[0]->role_id);
            //调用指定部门下的文章
            if($user->department->id()) {
                foreach ($user->roles as $role) {
                    if($role->call_departments->id()) {
                        $idset = FUNC::setArrayByField(array('depart_id'), $role->call_departments, false);
                        $idset[] = $user->department->depart_id;
                        $condi[] = new QDB_Expr(
                    'depart_id in(' .implode(',',$idset). ')'
                        );
                    }
                    else {
                        $condi[] = new QDB_Expr(
                    'depart_id in(' .$user->department->depart_id. ')'
                        );
                    }
                }

            }
/*
            if($orcols) {
                $orwhere[] = new QDB_Expr(
            'col_id in('. implode(',', $orcols) . ')'
                );

                $orwhere[] = new QDB_Expr(
            'col_id in('. implode(',', $orcols) . ')'
                );
            }
*/
            //            $condi[] = new QDB_Expr(
            //                                    'role_id in('. implode(',', FUNC::setArrayByField(array('role_id'), $user->roles)) . ')'
            //            );
            //            if($childrens) {
            //                $condi[] = new QDB_Expr(
            //            'col_id in(' .implode(',', $childrens). ')'
            //                );
            //            }
        }
        else {
            if($childrens) {
                $condi[] = new QDB_Expr(
            'col_id in(' .implode(',', $childrens). ')'
                );
            }
            /*
            if($user->department->id()) {
                $condi[] = new QDB_Cond(
                    'depart_id =? ', $user->department->depart_id
                );
            }
            */
        }

        //        dump($nodes);
        //            dump(array_merge($nodes, $cols));
        //        if(in_array(array($this->_attrs['col_id']), $nodes)) echo 'ok';
//                dump($condi);
        $pagesize = $this->_attrs['pagesize']?$this->_attrs['pagesize']:10;
        $articles = Article::find($condi)->orWhere($orwhere)->top($pagesize)->order('created DESC')->query();
        $records = Article::find($condi)->all()->getCount('art_id');

        //            $assoc_view_users = $articles->post->meta()->assoc('view_users');
        //            $assoc_view_users -> on_find_where = 'user_id='.$user->user_id;
        //            dump($articles);
        $this->_view['articles'] = $articles;
        $this->_view['user'] = $user;
        $this->_view['attrs'] = $this->_attrs;

        $this->_view['records'] = $records;
        $this->_view['pagesize'] = $pagesize;

        if(!$this->_attrs['viewname']) {
            $view = 'call_articles';
        }
        else {
            $view = $this->_attrs['viewname'];
        }
        return $this->_fetchView('view:' .Helper_Common::currentStyle(). '/_control/'.$view);
    }

}
?>